/*****************************************************\
* Copyright (c) 2020 The game2d Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.

* Author:         dzlua - https://gitee.com/dzlua/game2d.git
* Email:          505544956@qq.com
* FileName:       core/sprite.cc
* Create Time:    2020/03/21 12:27:34
* Description:    /
\****************************************************/
#include "sprite.h"

namespace g2d {
  void Sprite::set_attribute(const char *name, const char *value) {
    if (strcmp(name, "size") == 0) {
      auto sz = utils::parse_size(value);
      rect_.w = sz.w; rect_.h = sz.h;
    } else if (strcmp(name, "pos") == 0) {
      auto pos = utils::parse_pos(value);
      rect_.x = pos.x; rect_.y = pos.y;
    } else if (strcmp(name, "visible") == 0) set_visible(utils::parse_bool(value));
    else if (strcmp(name, "enable") == 0) set_enable(utils::parse_bool(value));
    else if (strcmp(name, "bkcolor") == 0) set_back_color(utils::parse_color(value));
    else if (strcmp(name, "image") == 0) set_image(&utils::parse_image(value));
  }

  void AniSprite::set_attribute(const char *name, const char *value) {
    if (strcmp(name, "interval") == 0) set_interval(utils::parse_uint32(value));
    else if (strcmp(name, "startframe") == 0) set_start_frame(utils::parse_uint32(value));
    else if (strcmp(name, "stopframe") == 0) set_start_frame(utils::parse_uint32(value));
    else if (strcmp(name, "curframe") == 0) set_start_frame(utils::parse_uint32(value));
    else if (strcmp(name, "start") == 0) { if (utils::parse_bool(value)) start(); }
    else if (strcmp(name, "multiimage") == 0) {
      auto a = utils::parse_image(value);
      if (a.src.w == 0) a.src.w = (int)rect_.w;
      if (a.src.h == 0) a.src.h = (int)rect_.h;
      set_image(a.id.c_str(), a.src.x, a.src.y, a.src.w, a.src.h);
    } else Sprite::set_attribute(name, value);
  }
} // end namespace g2d